package org.cladwell.controller;

import org.Util.RSAUtil;
import org.cladwell.UserService;
import org.cladwell.controller.support.UserSupport;
import org.cladwell.domain.JsonResponce;
import org.cladwell.domain.User;
import org.cladwell.domain.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.interfaces.RSAPublicKey;

@RestController
public class UserApi {
    @Autowired
    private UserService service;
    @Autowired
    private UserSupport support;
    @GetMapping("users")
    public JsonResponce<User> getUserInfo(){
        Long userid=UserSupport.getCurrentUserid();
        User user=service.getUserInfo(userid);
        return new JsonResponce<>(user);
    }
    @GetMapping("/rsa-pks")
    public JsonResponce<String> getRsaPublicKey()  {
        String publicKey = RSAUtil.getPublicKeyStr();
        return new JsonResponce<>(publicKey);
    }
    @PostMapping("/users")
    public JsonResponce<String> addUser(@RequestBody User user){
        service.addUser(user);
        return JsonResponce.success("成功");
    }
    @PostMapping("/user-tokens")
    public JsonResponce<String> login(@RequestBody User user) throws Exception {
       String token= service.login(user);
        return JsonResponce.success(token);
    }
    @PutMapping("/user-infos")
    public JsonResponce<String> updataUserInfos(@RequestBody Userinfo userinfo){
        Long userid = UserSupport.getCurrentUserid();
        userinfo.setUserId(userid);
        service.updatauserinfo(userinfo);
        return JsonResponce.success("更新成功");
    }

}
